package com.parsifal.bluetoothdemo.ui;

import android.bluetooth.BluetoothDevice;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.parsifal.bluetoothdemo.BluetoothPresenter;
import com.parsifal.bluetoothdemo.R;
import com.parsifal.bluetoothdemo.iface.IBluetoothView;
import com.parsifal.bluetoothdemo.iface.IBluetoothViewCallback;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements IBluetoothView {

    private static final String TAG = "MainActivity";

    private ToggleButton mToggleButton = null;

    private Button mBtnOpenBt = null;

    private Button mBtnSearchDevices = null;

    private ProgressBar mPbSearching = null;

    private ListView mLvDevices = null;

    private DevicesAdapter mDevicesAdapter = null;

    private IBluetoothViewCallback mViewCallback = null;

    private ChoiceDialog mChoiceDialog = null;

    private ClientDialog mClientDialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToggleButton = (ToggleButton) findViewById(R.id.toggle_state);
        mBtnOpenBt = (Button) findViewById(R.id.btn_open_bt);
        mBtnSearchDevices = (Button) findViewById(R.id.btn_search_devices);
        mLvDevices = (ListView) findViewById(R.id.lv_devices);
        mPbSearching = (ProgressBar) findViewById(R.id.pb_searching);

        mViewCallback = new BluetoothPresenter(this);
        mViewCallback.onWidgetsInitialized();

        mToggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mViewCallback.onCheckedChanged(buttonView, isChecked);
            }
        });
        mToggleButton.setChecked(true);

        mDevicesAdapter = new DevicesAdapter(MainActivity.this);
        mLvDevices.setAdapter(mDevicesAdapter);
        mLvDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mViewCallback.onItemClick(parent, view, position, id);
            }
        });

        mBtnOpenBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewCallback.onClick(v);
            }
        });

        mBtnSearchDevices.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewCallback.onClick(v);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        mViewCallback.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mViewCallback.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mViewCallback.onDestroy();
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        mViewCallback.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void disabledSearchButton() {
        mBtnSearchDevices.setEnabled(false);
    }

    @Override
    public void enabledSearchButton() {
        mBtnSearchDevices.setEnabled(true);
    }

    @Override
    public void showProgressBar() {
        mPbSearching.setVisibility(View.VISIBLE);
    }

    @Override
    public void dismissProgressBar() {
        mPbSearching.setVisibility(View.GONE);
    }

    @Override
    public void clearListView() {
        mDevicesAdapter.clear();
    }

    @Override
    public void updateListView(ArrayList<BluetoothDevice> mDevices) {
        mDevicesAdapter.fill(mDevices);
    }

    @Override
    public void setHighLight(ArrayList<BluetoothDevice> devices) {
        mDevicesAdapter.setHighLight(devices);
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showChoiceDialog() {
        mChoiceDialog = new ChoiceDialog(this);
        mChoiceDialog.setButton1OnclickListener(null, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewCallback.onClick(v);
            }
        });
        mChoiceDialog.setButton2OnclickListener(null, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewCallback.onClick(v);
            }
        });
        mChoiceDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mViewCallback.onDismiss(dialog, ChoiceDialog.ID);
            }
        });
        mChoiceDialog.show();
    }

    @Override
    public void dismissChoiceDialog() {
        if (null != mChoiceDialog) {
            mChoiceDialog.dismiss();
            mChoiceDialog = null;
        }
    }

    @Override
    public String getEditText() {
        return mClientDialog.getEditText();
    }

    @Override
    public void showClientDialog() {
        mClientDialog = new ClientDialog(this);
        mClientDialog.setButtonOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewCallback.onClick(v);
            }
        });
        mClientDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                mViewCallback.onDismiss(dialog, ClientDialog.ID);
            }
        });
        mClientDialog.show();
    }

    @Override
    public void dismissClientDialog() {
        if (null != mClientDialog) {
            mClientDialog.dismiss();
            mClientDialog = null;
        }
    }

    @Override
    public void addClientMessage(String msg) {
        mClientDialog.addClientMessage(msg);
    }

    @Override
    public void addServerMessage(String msg) {
        mClientDialog.addServerMessage(msg);
    }

    @Override
    public void postRunnable(Runnable runnable) {
        runOnUiThread(runnable);
    }
}
